

import java.util.Arrays;
import java.util.Scanner;
public class Text03 {
	public static void main(String[] args) {
		 Scanner in = new Scanner(System.in);
		 int n=in.nextInt();
		 String []id=new String[n];
		 for(int i=0;i<n;i++){
		       id[i]=in.next();
			}
		 String []id1=new String[n];
		 while(in.hasNext()){
			 String str=in.next();
				if(str.equals("sort1")) {
					 for(int i=0;i<n;i++){
					       id1[i]=id[i].substring(6,14); 
					 }
					  Arrays.sort(id1);
					 
					  for(int i=0;i<n;i++){
		                  System.out.println(id1[i].substring(0,4)+"-"+id1[i].substring(4,6)+"-"+id1[i].substring(6,8));
					  }
				} 
				else if(str.equals("sort2")){
					 for(int i=0;i<n;i++){
					       id1[i]=id[i].substring(6,14); 
					 }
					  Arrays.sort(id1);
					  for(int i=0;i<n;i++){
						  for(int j=0;j<n;j++){
							  if(id1[i].equals(id[j].substring(6,14))){
								  System.out.println(id[j]);
								  break;
							  } 
						  }		  
					  }
				} 
				else {
						System.out.println("exit");	
					}	 
				}
		 }	 
	}
 
